package cn.com.api.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.api.entity.ApiT_member_entrepreneur;

import cn.com.api.service.ApiMemberService;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.ApiT_member_partnerService;
import cn.com.api.service.CheckService;
import cn.com.util.*;

/**
 * 
 * ClassName: ApiMemberEntrepreneurController
 * 
 * @Description: 企业主模块接口
 * @author yewl
 * @date 2016年12月29日
 */
@Controller
@RequestMapping(Utils.SYS_API + "entrepreneur/")
public class ApiMemberEntrepreneurController {

	@Autowired
	private ApiT_member_entrepreneurService apiT_member_entrepreneurService;
	@Autowired
	private CheckService checkService;

	private static Logger logger = Logger.getLogger(ApiMemberEntrepreneurController.class);

	/**
	 * 申请成为企业主
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @param entrepreneur
	 *            接收申请企业主信息资料的实体类，接口参数名为实体属性名即可
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "applyEntrepreneur")
	public void applyEntrepreneur(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, ApiT_member_entrepreneur entrepreneur) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, String> paramMap = new HashMap<String, String>();
		try {
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();

				map = apiT_member_entrepreneurService.applyEntrepreneur(entrepreneur);

			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 获取申请成为企业主的申请信息
	 * 
	 * @param user_id
	 *            用户id
	 * @param token
	 * 
	 * @return
	 * @throws Exception
	 */
	@Transactional
	@RequestMapping(value = "getEntrepreneur")
	public void getEntrepreneur(HttpServletRequest request, HttpServletResponse response, String user_id, String token)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.put("user_id", user_id);
				PageInfo page = new PageInfo();
				page.setCurrentPage(1);
				page.setShowCount(5);
				List<ApiT_member_entrepreneur> list = apiT_member_entrepreneurService
						.findT_member_entrepreneursByMap(map, page);
				map.clear();
				if (list != null && list.size() > 0) {
					map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
					map.put(Utils.MESSAGE, "操作成功");
					map.put(Utils.DATAS, list.get(0));
				} else {
					map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
					map.put(Utils.MESSAGE, "无申请记录");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(Util.getStackTraceAsString(e));
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	@RequestMapping(value = "findU_memberEntrepreneur_Info")
	public void findU_memberEntrepreneur_Info(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		try {
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			// if (checkService.checkToken(user_id, token, map) &&
			// checkService.checkParams(paramMap, map)) {
			map.put("user_id", user_id);

			List<Map<String, Object>> list = apiT_member_entrepreneurService.findMember_entrepreneursByMap(map);
			for (Map<String, Object> map2 : list) {
				resultMap = new HashMap<String, Object>();
				resultMap.put("id", map2.get("ID") + "");
				resultMap.put("corp_fname", map2.get("CORP_FNAME"));
				resultMap.put("corp_sname", map2.get("CORP_SNAME"));
				resultMap.put("corp_brief", map2.get("CORP_BRIEF"));

				resultList.add(resultMap);
			}

			map.clear();
			if (resultList != null && resultList.size() > 0) {
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
				map.put(Utils.DATAS, resultList);
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
				map.put(Utils.MESSAGE, "无公司记录");
			}
			// }
		} catch (Exception e) {
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));

	}

	@RequestMapping(value = "updateU_memberEntrepreneur_Info")
	public void updateU_memberEntrepreneur_Info(HttpServletRequest request, HttpServletResponse response,
			String user_id, String token) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String entrepreneur_id = request.getParameter("entrepreneur_id");
			String corp_brief = request.getParameter("corp_brief");
			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			// if (checkService.checkToken(user_id, token, map) &&
			// checkService.checkParams(paramMap, map)) {

			ApiT_member_entrepreneur t_member_entrepreneur = new ApiT_member_entrepreneur();
			t_member_entrepreneur.setId(Integer.parseInt(entrepreneur_id));
			t_member_entrepreneur.setCorp_brief(corp_brief);
			int result = apiT_member_entrepreneurService.updateT_member_entrepreneur_brief(t_member_entrepreneur);
			if (result >= 1) {
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			} else {
				map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
				map.put(Utils.MESSAGE, "系统异常");
			}

			// }
		} catch (Exception e) {
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * @Title: getEntrepreneurList
	 * @Description: 获取企业主列表(聊天时用)
	 */
	@RequestMapping(value = "getEntrepreneurList")
	public void getEntrepreneurList(HttpServletRequest request, HttpServletResponse response, String user_id,
			String token, String keyword, PageInfo page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		try {

			Map<String, String> paramMap = new HashMap<String, String>();
			paramMap.put("u_id", user_id);
			paramMap.put("token", token);
			if (checkService.checkToken(user_id, token, map) && checkService.checkParams(paramMap, map)) {
				map.clear();
				List<ApiT_member_entrepreneur> list = apiT_member_entrepreneurService.getEntrepreneurList(keyword,
						page);
				map.put(Utils.DATAS, list);
				map.put(Utils.Page, page);
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "操作成功");
			}
		} catch (Exception e) {
			map.put(Utils.RESULT, Utils.RESULT_RC_EXCEPTION);
			map.put(Utils.MESSAGE, "系统异常");
		}
		JsonUtil.printJson(response, JsonUtil.objectToJsonStr(map));
	}

}
